/*
 * Copyright (C) 2009 Georgios J. Georgopoulos, All rights reserved.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.gwt.mosaic.xul.client.ui;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author georgopoulos.georgios(at)gmail.com
 * 
 */
public class Textarea extends Element {
  private static final long serialVersionUID = 2959543842741281287L;

  private transient TextArea textArea = null;

  // -----------------------------------------------------------------------
  /**
   * The number of columns to display.
   */
  public static final String COLS = "cols";

  public int getCols() {
    return getInteger(COLS);
  }

  public void setCols(int cols) {
    putInteger(COLS, cols);
  }

  // -----------------------------------------------------------------------
  /**
   * Indicates whether the element is disabled or not. If this element is set to
   * true the element is disabled. Disabled elements are usually drawn with
   * grayed-out text. If the element is disabled, it does not respond to user
   * actions, it cannot be focused, and the command event will not fire.
   */
  public static final String DISABLED = "disabled";

  public boolean isDisabled() {
    return getBoolean(DISABLED);
  }

  public void setDisabled(boolean disabled) {
    putBoolean(DISABLED, disabled);
  }

  // -----------------------------------------------------------------------
  /**
   * The number of rows to display in the element. If the element contains more
   * than this number of rows, a scrollbar will appear which the user can use to
   * scroll to the other rows.
   */
  public static final String ROWS = "rows";

  public int getRows() {
    return getInteger(ROWS);
  }

  public void setRows(int rows) {
    putInteger(ROWS, rows);
  }

  // -----------------------------------------------------------------------
  /**
   * The default value entered in a textarea.
   */
  public static final String VALUE = "value";

  public String getValue() {
    return getString(VALUE);
  }

  public void setValue(String value) {
    putString(VALUE, value);
  }

  // -----------------------------------------------------------------------
  public Textarea() {
    super();
  }

  // -----------------------------------------------------------------------
  @Override
  protected Widget createUI() {
    if (textArea == null) {
      textArea = new TextArea();
      textArea.addChangeHandler(new ChangeHandler() {
        public void onChange(ChangeEvent event) {
          setValue(textArea.getValue());
        }
      });
    }

    syncUI(textArea);

    return textArea;
  }

  // -----------------------------------------------------------------------
  @Override
  protected void syncUI(Widget ui) {
    super.syncUI(ui);
    
    if (attributeMap.containsKey(COLS)) {
      textArea.setCharacterWidth(getCols());
    }
    
    textArea.setEnabled(!isDisabled());
    
    if (attributeMap.containsKey(ROWS)) {
      textArea.setVisibleLines(getRows());
    }

    if (attributeMap.containsKey(VALUE)) {
      textArea.setValue(getValue());
    }
  }

}
